#!/usr/bin/env node

const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')

function copyTmpl(dir, dDir, answer){
    fs.readdir(dir, (err, files) => {
        files.forEach(file => {
            // 获取该文件完整路径
            let filePath = path.join(dir, file)
            let destPath = path.join(dDir, file)

            // 判断该文件是否为文件夹
            let is_direc = fs.lstatSync(filePath).isDirectory()
            // console.log('--------' + filePath + '--------' + is_direc.toString())

            // 是文件夹就递归，不是就写入文件
            if(is_direc){
                fs.mkdirSync(destPath)
                copyTmpl(filePath, destPath, answer)
            } else {
                ejs.renderFile(filePath, answer, (err, result) => {
                    fs.writeFileSync((destPath), result)
                })
            }
        })
    })
}

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'Your pro name?',
    }
])
.then(answer => {
    const tmplDir = path.join(__dirname, 'templates')
    const destDir = path.join(process.cwd())
    copyTmpl(tmplDir, destDir, answer)
})